<template>
    <div class="main-body">
        <div class="top-box">
            <div>
                <el-button type="primary" @click="saveDetail">保存填报</el-button>
                <el-button @click="countWnv">开始评估</el-button>
            </div>
            <div class="main-title" style="margin-left: 120px" >
                牛海绵状脑病（BSE）风险数据收集
            </div>
            <div>
                <el-date-picker
                    v-model="wnvMonth"
                    type="year"
                    format="yyyy"
                    value-format="yyyy"
                    :clearable="false"
                    placeholder="选择">
                </el-date-picker>
                <el-select v-model="wnvArea" filterable placeholder="请选择所属国家" style="margin: 0 10px;">
                    <el-option :label="area.areaName" :value="area.areaId" v-for="(area,idx) of areasList" :key="idx"></el-option>
                </el-select>
                <el-button type="primary" @click="getList">查询</el-button>
            </div>
        </div>
        <div class="table-box">
            <div v-for="(item,index) of wnvMainDetail" :key="index">
                <div class="title">{{item.indexNo}} {{item.indexDesc}}【{{item.biz2 || 0}}%】</div>
                <div class="edit-item">
                    <div v-for="(child,idx) of item.children" :key="idx" class="edit-item-item" :style="{'height': (child.children.length === 0 ? 1 : child.children.length) * 60 + 'px'}">
                        <template v-if="child.children.length === 0">
                            <div class="label item">{{child.indexNo}} {{child.indexDesc}}【{{child.biz2 || 0}}%】</div>
                            <div class="value item">
                                <el-input v-model="child.indexOptions" placeholder="请输入" type="number" v-if="child.biz3 === 'N' && child.biz1 === 'NUM'"></el-input>
                                <el-select v-model="child.indexOptions" placeholder="请选择" v-else>
                                    <el-option
                                        v-for="item in child.optionsList"
                                        :key="item.optionsCode"
                                        :label="item.optionsDesc + ' - ' + item.optionsScore + '(分)'"
                                        :value="item.optionsCode">
                                    </el-option>
                                </el-select>
                            </div>
                        </template>
                        <template v-else>
                            <div class="label item">{{child.indexNo}}.{{idx+1}} {{child.indexDesc}}【{{child.biz2 || 0}}%】</div>
                            <div class="third-item edit-item">
                                <div v-for="(ch,i) of child.children" :key="i" class="edit-item-item" style="width: 100%;">
                                    <div class="label item">{{ch.indexNo}} {{ch.indexDesc}}【{{ch.biz2 || 0}}%】</div>
                                    <div class="value item">
                                        <el-input v-model="ch.indexOptions" placeholder="请输入" type="number" v-if="ch.biz3 === 'N' && ch.biz1 === 'NUM'"></el-input>
                                        <el-select v-model="ch.indexOptions" placeholder="请选择" v-else>
                                            <el-option
                                                v-for="item in ch.optionsList"
                                                :key="item.optionsCode"
                                                :label="item.optionsDesc + ' - ' + item.optionsScore + '(分)'"
                                                :value="item.optionsCode">
                                            </el-option>
                                        </el-select>
                                    </div>
                                </div>
                            </div>
                        </template>
                    </div>
                </div>
            </div>
            <div v-if="wnvMainDetail">
                <div class="title border-top-none">经过权重计算{{this.biz3}}最终得分【{{this.WNVResource}}】</div>
                <!--
                 <div class="edit-item">
                    <div class="edit-item-item" style="width: 100%">
                        <div class="label item border-bottom-none" style="max-width: 200px">
                            总体评估
                        </div>
                        <div class="value item border-bottom-none">
                            <el-input v-model="WNVResourceDec"></el-input>
                        </div>
                    </div>

                </div>

                <div class="edit-item">
                  <div class="edit-item-item" style="width: 100%">
                    <div class="label item border-bottom-none" style="max-width: 200px">
                      相关建议
                    </div>
                    <div class="value item border-bottom-none">
                      <el-input v-model="biz2"></el-input>
                    </div>
                  </div>

                </div>
                -->
            </div>
            <div v-else class="empty">
                请先初始化数据
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            wnvArea: "",
            // wnvMonth: wfy.format('yyyy-MM', new Date()),
            wnvMonth: String(new Date().getFullYear()),
            wnvMainDetail: null,
            WNVResource: 0,
            wnvStatus: "",
            miu: "",
            wnvMainId: "",
            WNVResourceDec: "",
            biz2: "",
            areasList: []
        }
    },
    components: {

    },
    created () {
    },
    activated () {

    },
    mounted () {
        this.getArea();
        // this.getList();
    },
    methods: {
        countWnv(){
            if(this.wnvStatus === "A"){
                return this.$message.warning("已审核，不能再次计算！")
            }
            console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
            let firstQz = 0;
            this.wnvMainDetail.forEach((item) => {
                firstQz += Number(item.biz2)
            })
            if(firstQz !== 100){
                return  this.$message.warning("一级指标的权重总计不为100，请检查指标权重后再进行评估！")
            }

            let twiceQz = "";
            this.wnvMainDetail.forEach((item) => {
                let qz = 0;
                item.children.forEach((child) => {
                    qz += Number(child.biz2)
                })
                if(qz !== Number(item.biz2)){
                    twiceQz = "一级指标【" + item.indexDesc + "】下属指标权重相加与该一级指标权重不相等！";
                }
            })
            if(twiceQz > 0){
                return this.$message.warning(twiceQz);
            }

            let thirdQz = "";
            this.wnvMainDetail.forEach((item) => {
                item.children.forEach((child) => {
                    let qz = 0;
                    child.children.forEach((ch) => {
                        qz += Number(ch.biz2)
                    })
                    if(qz !== Number(child.biz2)){
                        thirdQz = "二级指标【" + item.indexDesc + "】下属指标权重相加与该二级指标权重不相等！";
                    }
                })
            })
            if(thirdQz > 0){
                return this.$message.warning(thirdQz);
            }


            this.WNVResource = 0;
            let emptyTargetList = [];
            this.wnvMainDetail.forEach((wnv) => {
                if(wnv.children && wnv.children.length > 0){
                    wnv.children.forEach((child) => {
                        if(child.children.length > 0){
                            child.children.forEach((ch) => {
                                if(!ch.indexOptions){
                                    emptyTargetList.push(ch.indexDesc)
                                }
                                if(ch.optionsList && ch.optionsList.length > 0 && ch.indexOptions){
                                    if(ch.biz3 === "N" && ch.biz1 === "NUM"){
                                        this.WNVResource += Number(ch.indexOptions) * Number(ch.biz2)
                                    }else{
                                        let scoreObj = ch.optionsList.find((item) => {
                                            return item.optionsCode === ch.indexOptions
                                        })
                                        this.WNVResource += Number(scoreObj.optionsScore) * Number(ch.biz2)
                                    }
                                }
                            })
                        }else{
                            if(!child.indexOptions){
                                emptyTargetList.push(child.indexDesc)
                            }
                            if(child.optionsList && child.optionsList.length > 0 && child.indexOptions){
                                if(child.biz3 === "N" && child.biz1 === "NUM"){
                                    this.WNVResource += Number(child.indexOptions) * Number(child.biz2)
                                }else{
                                    let scoreObj = child.optionsList.find((item) => {
                                        return item.optionsCode === child.indexOptions
                                    })
                                    this.WNVResource += Number(scoreObj.optionsScore) * Number(child.biz2)
                                }
                            }
                        }
                    })
                }
            })
            if(emptyTargetList.length > 0){
                return this.$message.warning("指标【" + emptyTargetList[0] + "】还未填写！")
            }
            this.WNVResource = wfy.accDiv(Number(this.WNVResource),100);
            this.$message.success("得分计算成功！总得分为" + this.WNVResource)
        },
        // 设置指标数据
        setTargetData(data){
            this.wnvMainId = data.wnvMainId;
            let dtlList = [];
            let levelNum = 0;
            data.wnvDtlList.forEach((item) => {
                if(item.indexKindId === "1"){
                    levelNum++;
                    dtlList.push({
                        ...item,
                        levelNum: levelNum,
                        children: []
                    })
                }
                if(item.indexKindId === "2"){
                    let findResult = dtlList.find((zb) => {
                        return zb.indexId === item.preindexId
                    })
                    let childResult = data.wnvDtlList.filter((zb) => {
                        return zb.preindexId === item.indexId
                    })
                    console.log("🚀 ~ childResult", childResult)
                    item.children = childResult;
                    findResult && findResult.children.push({
                        ...item,
                        levelNum: levelNum,
                        children: childResult
                    })
                }
            })
            levelNum++;
            // dtlList.push({
            //     indexDesc: "三级指标",
            //     levelNum: levelNum,
            //     indexKindId: "3",
            //     children: []
            // })
            levelNum++;
            // dtlList.push({
            //     indexDesc: "常量指标",
            //     levelNum: levelNum,
            //     indexKindId: "C",
            //     children: []
            // })
            data.wnvDtlList.forEach((item) => {
                // if(item.indexKindId === "3"){
                //     let findResult = dtlList.find((zb) => {
                //         return zb.indexKindId === "3"
                //     })
                //     findResult.children.push(item)
                // }
                if(item.indexKindId === "C"){
                    let findResult = dtlList.find((zb) => {
                        return zb.indexKindId === "C"
                    })
                    console.log("🚀 ~ findResult", findResult)
                    findResult && findResult.children.push(item)
                }
            })
            return dtlList
        },
        saveDetail(){
            if(this.wnvStatus === "A"){
                return this.$message.warning("已审核，不能再次保存！")
            }
            console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
            if(wfy.zeroEmpty(this.WNVResource)){
                return this.$message.warning("请先点击【开始评估】按钮进行评估后再保存！");
            }
            let saveData = [];
            this.wnvMainDetail.forEach((item) => {
                item.children.forEach((child) => {
                    if(child.children.length === 0){
                        if(child.biz3 === "N" && child.biz1 === "NUM"){
                            saveData.push({
                                indexOptions: child.indexOptions,
                                wnvDtlId: child.wnvDtlId,
                                indexOptionsScore: 0
                            })
                        }else{
                            let option = child.optionsList.find((op) => {
                                return op.optionsCode === child.indexOptions
                            })
                            saveData.push({
                                indexOptions: child.indexOptions,
                                wnvDtlId: child.wnvDtlId,
                                indexOptionsScore: option.optionsScore
                            })
                        }
                    }else{
                        child.children.forEach((ch) => {
                            if(ch.biz3 === "N" && ch.biz1 === "NUM"){
                                saveData.push({
                                    indexOptions: ch.indexOptions,
                                    wnvDtlId: ch.wnvDtlId,
                                    indexOptionsScore: 0
                                })
                            }else{
                                let option = ch.optionsList.find((op) => {
                                    return op.optionsCode === ch.indexOptions
                                })
                                saveData.push({
                                    indexOptions: ch.indexOptions,
                                    wnvDtlId: ch.wnvDtlId,
                                    indexOptionsScore: option.optionsScore
                                })
                            }
                        })
                    }

                })
            })
            let area = this.areasList.find((a) => {
                return a.areaId === this.wnvArea
            })
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/updWnvDtl`),
                method: 'post',
                data: this.$http.adornData({
                    wnvDtlList: saveData,
                    wnvStatus: "C",
                    wnvMonth: this.wnvMonth,
                    wnvArea: this.wnvArea,
                    biz3: area.areaName,
                    wnvMainId: this.wnvMainId,
                    wnvComTime: wfy.format('yyyy-MM-dd HH:mm:ss', new Date()),
                    wnvResult: this.WNVResource,
                    wnvResultDesc: this.WNVResourceDec,
                    biz2: this.biz2
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 保存', data)
                if(data.wnvResult){
                    this.$alert("填报成功","友情提醒");
                }
            })
        },
        listAreaByParentId (pid) {
            if (!pid) pid = 0
            return this.$http({
                url: this.$http.adornUrl(`/sys/area/nav`),
                method: 'get',
                params: this.$http.adornParams({})
            })
        },
        getArea(){
            this.listAreaByParentId().then(({ data }) => {
                console.log("🚀 ~ data", data.length)
                // data.forEach((row) => {
                //     if(row.level === 1){
                //         this.areasOptions.push({
                //             value: row.areaId,
                //             label: row.areaName,
                //             children: []
                //         })
                //     }
                // })
                data.forEach((row) => {
                    if(row.level === 2){
                        // this.areasOptions.forEach((op) => {
                        //     if(row.parentId === op.value){
                        //         op.children.push({
                        //             value: row.areaId,
                        //             label: row.areaName,
                        //         })
                        //     }
                        // })
                        this.areasList.push(row)
                    }
                })
                console.log("🚀 ~ this.areasList", this.areasList)
                this.wnvArea = this.areasList[0].areaId;
                this.getList()
            })
        },
        getList(){

            var loading = this.$loading();
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/queryWnv`),
                method: 'post',
                data: this.$http.adornData({
                    wnvMonth: this.wnvMonth,
                    wnvArea: this.wnvArea
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 查询是否有', data)
                loading.close();
                if(wfy.empty(data.wnvMainId)){
                    this.$confirm('该月份还没有填报数据，是否初始化数据并填报?', '友情提醒', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.initMonth()
                    }).catch(() => { })
                }else{
                    this.wnvMainDetail = this.setTargetData(data);
                    console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
                    this.WNVResource = data.wnvResult;
                    this.wnvStatus = data.wnvStatus;
                    this.WNVResourceDec = data.wnvResultDesc;
                    this.biz2 = data.biz2;
                }
            })
        },
        initMonth(){
            var loading = this.$loading();
            let area = this.areasList.find((a) => {
                return a.areaId === this.wnvArea
            })
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/initWnv`),
                method: 'post',
                data: this.$http.adornData({
                    wnvMonth: this.wnvMonth,
                    wnvArea: this.wnvArea,
                    biz3: area.areaName,
                    wnvYear: this.wnvMonth.substring(0,4),
                    wnvMainId: wfy.uuid(32)
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 初始化', data)
                loading.close();
                this.wnvMainDetail = this.setTargetData(data);
                console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.main-body{
    height: 100%;
    width: 100%;
}
.main-title{
    height: 40px;
    //width: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
}
.top-box{
    display: flex;
    justify-content: space-between;
}
.table-box{
    height: calc(100% - 40px);
    width: 100%;
    margin-top: 20px;
    border: 1px solid #dddddd;
}
.title{
    background-color: #f2f2f2;
    height: 50px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #dddddd;
    //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
    padding: 8px;
}
.edit-item{
    width: 100%;
    height: auto;
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    //.item{
    //    border-right: 1px solid #dddddd;
    //    border-bottom: 1px solid #dddddd;
    //}
    .item:last-child{
        border-right: none;
    }
    .label{
        width: 50%;
        height: 100%;
        display: flex;
        align-items: center;
        //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
        padding: 8px;
    }
    .value{
        width: calc(100% - 200px);
        min-width: 100px;
        height: 100%;
        //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
        padding: 8px;
        box-sizing: border-box;
    }
    .el-select{
        width: 100%;
        height: 100%;
    }
    .edit-item-item{
        width: 50%;
        height: 60px;
        display: flex;
    }
}
.source{
    height: 80px;
    width: 100%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    border-top: 1px solid #dddddd;
}
.border-top-none{
    border-top: none;
}
.border-bottom-none{
    border-bottom: none !important;
}
.item-img{
    height: 47px;
}
.empty{
    height: 300px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}
.third-item{
    display: flex;
    flex-direction: column;
    width: 100%;

}
</style>
